Page({
  data: {
    activeTab: 'home',
    records: [
      {
        id: '1',
        boatNumber: '鲁即渔12375',
        location: '青岛海域',
        date: '2024-03-20'
      },
      {
        id: '2',
        boatNumber: '鲁即渔12376',
        location: '威海海域',
        date: '2024-03-19'
      }
    ],
    fishingRecords: [
      {
        id: 1,
        title: '东海捕捞作业',
        desc: '今天的海上作业非常顺利，共捕获各类鱼获若干，整体情况良好，天气晴朗，海况平稳。',
        image: '/assets/images/fishing1.jpg',
        time: '2024-01-10',
        location: '东海海域'
      },
      {
        id: 2,
        title: '东海捕捞作业',
        desc: '今天的海上作业非常顺利，共捕获各类鱼获若干，整体情况良好，天气晴朗，海况平稳。',
        image: '/assets/images/fishing2.jpg',
        time: '2024-01-11',
        location: '东海海域'
      },
      {
        id: 3,
        title: '东海捕捞作业',
        desc: '今天的海上作业非常顺利，共捕获各类鱼获若干，整体情况良好，天气晴朗，海况平稳。',
        image: '/assets/images/fishing3.jpg',
        time: '2024-01-12',
        location: '东海海域'
      },
      {
        id: 4,
        title: '东海捕捞作业',
        desc: '今天的海上作业非常顺利，共捕获各类鱼获若干，整体情况良好，天气晴朗，海况平稳。',
        image: '/assets/images/fishing4.jpg',
        time: '2024-01-13',
        location: '东海海域'
      }
    ],
    searchValue: '',
    originalRecords: [] // 用于存储原始数据
  },

  onLoad() {
    // 检查登录状态
    const token = wx.getStorageSync('token');
    if (!token) {
      wx.redirectTo({
        url: '/pages/login/index'
      });
    }

    // 保存原始数据
    this.setData({
      originalRecords: this.data.fishingRecords
    });
  },

  onShow() {
    // 只在首页设置 selected: 0，其他页面请设置对应的 selected 值
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 0
      });
    }
  },

  // 底部导航切换
  onTabBarChange(e) {
    const value = e.detail.value;
    
    // 如果点击的是拍照按钮
    if (value === 'camera') {
      this.takePhoto();
      return;
    }

    // 其他导航项的跳转
    const routes = {
      home: '/pages/home/index',
      boat: '/pages/boat/index',
      fishing: '/pages/fishing/index',
      profile: '/pages/profile/index'
    };

    if (routes[value]) {
      wx.switchTab({
        url: routes[value]
      });
    }
  },

  // 拍照功能
  takePhoto() {
    wx.chooseMedia({
      count: 1,
      mediaType: ['image'],
      sourceType: ['camera'],
      camera: 'back',
      success: (res) => {
        const tempFilePath = res.tempFiles[0].tempFilePath;
        // 上传图片
        this.uploadImage(tempFilePath);
      }
    });
  },

  // 上传图片
  uploadImage(filePath) {
    wx.showLoading({
      title: '正在上传...'
    });

    wx.uploadFile({
      url: 'https://api.example.com/upload',
      filePath: filePath,
      name: 'file',
      success: (res) => {
        wx.hideLoading();
        if (res.statusCode === 200) {
          wx.showToast({
            title: '上传成功',
            icon: 'success'
          });
          // 可以在这里处理上传成功后的逻辑
        } else {
          wx.showToast({
            title: '上传失败',
            icon: 'error'
          });
        }
      },
      fail: () => {
        wx.hideLoading();
        wx.showToast({
          title: '上传失败',
          icon: 'error'
        });
      }
    });
  },

  navigateToBoat() {
    wx.switchTab({
      url: '/pages/boat/index'
    });
  },

  navigateToFishing() {
    wx.switchTab({
      url: '/pages/fishing/index'
    });
  },

  navigateToQrcode() {
    wx.navigateTo({
      url: '/pages/qrcode/index'
    });
  },

  navigateToCertification() {
    wx.navigateTo({
      url: '/pages/certification/index'
    });
  },

  // 快捷操作方法
  addBoat() {
    wx.navigateTo({
      url: '/pages/boat/add/index'
    });
  },

  addFishingRecord() {
    wx.navigateTo({
      url: '/pages/fishing/add/index'
    });
  },

  scanCode() {
    wx.scanCode({
      success: (res) => {
        // 处理扫码结果
        console.log(res);
        wx.showToast({
          title: '扫码成功',
          icon: 'success'
        });
      },
      fail: () => {
        wx.showToast({
          title: '扫码失败',
          icon: 'error'
        });
      }
    });
  },

  viewAllRecords() {
    wx.navigateTo({
      url: '/pages/fishing/index'
    });
  },

  // 搜索框输入变化
  onSearchChange(e) {
    const value = e.detail.value;
    this.setData({ searchValue: value });
    this.searchRecords(value);
  },

  // 清空搜索
  onSearchClear() {
    this.setData({
      searchValue: '',
      fishingRecords: this.data.originalRecords
    });
  },

  // 搜索提交
  onSearchSubmit(e) {
    const value = e.detail.value;
    this.searchRecords(value);
  },

  // 搜索记录
  searchRecords(keyword) {
    if (!keyword) {
      this.setData({
        fishingRecords: this.data.originalRecords
      });
      return;
    }

    const filteredRecords = this.data.originalRecords.filter(record => {
      return record.title.includes(keyword) || 
             record.desc.includes(keyword) || 
             record.location.includes(keyword);
    });

    this.setData({
      fishingRecords: filteredRecords
    });
  },

  // 点击记录项
  onRecordTap(e) {
    const recordId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/fishing/detail/index?id=${recordId}`
    });
  }
}); 